package com.marvels.financial.loan.controller;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.dto.pay.McfePaymentUserBindCard;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.financial.loan.dto.HandWithholdConditionDTO;
import com.marvels.financial.loan.dto.HandWithholdListDTO;
import com.marvels.financial.loan.dto.HandleWithholdResultDTO;
import com.marvels.financial.loan.service.HandleWithholdApi;

import cn.hutool.core.util.StrUtil;



/**
 * 手工代扣
 * @author tangpw
 *
 */
@RestController
@RequestMapping("financial/loan/handleWithhold/service")
public class HandleWithholdController {
	
	@Autowired
	private HandleWithholdApi withholdApi;
	
	
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		dateFormat.setLenient(false);
		binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	}
	
	
	/**
	 * 手工代扣-代扣还款-申请列表
	 * @return
	 */
	@RequestMapping("/applyList")
	@ResponseBody
	public MarvelsResultDTO applyList(@RequestBody HandWithholdConditionDTO dto){
		try {
			Page<HandWithholdListDTO> list=withholdApi.applyList(dto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), list);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询手工代扣-代扣还款-申请数据FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询手工代扣-代扣还款-申请数据异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "查询手工代扣-代扣还款-申请数据异常", null);
		}
	}
	
	
	
	/**
	 * 手工代扣-代扣还款-申请操作
	 * @return
	 */
	@RequestMapping("/handleWithholeApply")
	@ResponseBody
	public MarvelsResultDTO handleWithholeApply(@RequestBody HandWithholdConditionDTO dto){
		Map<String,Integer> result = null;
		if(dto != null && dto.getIndex() != null){
			result = new HashMap<String,Integer>();
			result.put("index", dto.getIndex());
		}
		try {
			withholdApi.handleWithholeApply(dto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), result);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>手工代扣-代扣还款-申请操作FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), result);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>手工代扣-代扣还款-申请操作异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "手工代扣-代扣还款-申请操作数据异常", result);
		}
	}
	
	
	
	
	/**
	 * 手工代扣-代扣还款-审核列表
	 * @return
	 */
	@RequestMapping("/handleWithholeReviewList")
	@ResponseBody
	public MarvelsResultDTO handleWithholeReviewList(@RequestBody HandWithholdConditionDTO dto){
		try {
			Page<HandWithholdListDTO> listDTOs=withholdApi.handleWithholeReviewList(dto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), listDTOs);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询手工代扣-代扣还款-审核列表FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询手工代扣-代扣还款-审核列表异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "查询手工代扣-代扣还款-审核列表异常", null);
		}
	}
	
	
	
	
	/**
	 * 手工代扣-代扣还款-审核拒绝
	 * @return
	 */
	@RequestMapping("/handleWithholeReviewRefuse")
	@ResponseBody
	public MarvelsResultDTO handleWithholeReviewRefuse(@RequestBody HandWithholdConditionDTO dto){
		try {
			withholdApi.handleWithholeReviewRefuse(dto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), null );
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询手工代扣-代扣还款-审核列表FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询手工代扣-代扣还款-审核列表异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "查询手工代扣-代扣还款-审核列表异常", null);
		}
	}
	
	
	
	
	
	/**
	 * 手工代扣-代扣还款-审核通过
	 * @return
	 */
	@RequestMapping("/handleWithholeReviewAdopt")
	@ResponseBody
	public MarvelsResultDTO handleWithholeReviewAdopt(@RequestBody HandWithholdConditionDTO dto){
		// 一键通过
		if(StrUtil.isNotBlank(dto.getLoanIds()) && !Arrays.asList(dto.getLoanIds().split(",")).isEmpty()){
			MarvelsLogUtil.getInstance().info("一键代扣loanIds:" + dto.getLoanIds());
			List<String> loanIdList = Arrays.asList(dto.getLoanIds().split(","));
			int count = loanIdList.size();
			int success = 0;
			for (String loanId : loanIdList) {
				HandWithholdConditionDTO param = new HandWithholdConditionDTO();
				param.setLoanId(loanId);
				try {
					withholdApi.handleWithholeApprove(param);
					success ++;
				} catch (Exception e) {
					MarvelsLogUtil.getInstance().error(">>>>>>>>>手工代扣（批量）-代扣还款-审核通过异常", e);
				}
			}
			if(success > 0){
				if(success == count){
					return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), null);
				}else{
					return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),"部分成功，选中：" + count + "单，成功：" + success + "单", null);
				}
			}else{
				return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), "处理失败", null);
			}
		}else{
			//普通通过
			try {
				withholdApi.handleWithholeApprove(dto);
				return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), null);
			} catch (MarvelsBusinessException e) {
				MarvelsLogUtil.getInstance().error(">>>>>>>>>手工代扣-代扣还款-审核通过FAIL:", e);
				return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
			} catch (Exception e) {
				MarvelsLogUtil.getInstance().error(">>>>>>>>>手工代扣-代扣还款-审核通过异常", e);
				return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "手工代扣-代扣还款-审核通过异常", null);
			}
		}
	}
	
	
	
	
	
	
	
	/**
	 * 手工代扣-代扣还款-审核结果
	 * @return
	 */
	@RequestMapping("/handleWithholeReviewResult")
	@ResponseBody
	public MarvelsResultDTO handleWithholeReviewResult(@RequestBody HandWithholdConditionDTO dto){
		try {
			List<HandleWithholdResultDTO> list=withholdApi.handleWithholeReviewResult(dto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), list);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询手工代扣-代扣还款-审核结果FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>查询手工代扣-代扣还款-审核结果异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "查询手工代扣-代扣还款-审核结果异常", null);
		}
	}
	
	
	/**
	 * 查询用户银行卡列表
	 * @param dto
	 * @return
	 */
	@RequestMapping("/queryUserBankList")
	@ResponseBody
	public MarvelsResultDTO queryUserBankList(@RequestBody McfePaymentUserBindCard dto){
		try {
			List<Map<String, Object>> bankList=  withholdApi.queryUserBankList(dto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), bankList);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>手工代扣-代扣还款-申请操作FAIL:", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(), e.getMessage(), null);
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>手工代扣-代扣还款-申请操作异常", e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(), "手工代扣-代扣还款-申请操作数据异常", null);
		}
	}
}
